package DB;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class LogDB implements LogDBMan
{
	SessionFactory seFac;
	
	public LogDB(SessionFactory sessionFactory)
	{
		seFac = sessionFactory;
	}
	
	/**
	 * this function add event to the log
	 * @param ev {@link String} event to the log
	 */
	public void addEvent(String ev)
	{
		Session se = seFac.openSession();
		SystemLog sl = new SystemLog();
		sl.setLogEvent(ev);
		se.save(sl);
		se.flush();
		se.close();
	}
	
	/**
	 * this function returns all the record in the log
	 * @return {@link ArrayList} that contains all the record in the log
	 */
	public ArrayList<String> getLog()
	{
		Session se = seFac.openSession();
		ArrayList<String> log = null;
		Query query = se.createQuery("from SystemLog");
		if(!query.list().isEmpty())
		{
			log = new ArrayList<String>();
			List <SystemLog> list = query.list();
			Iterator<SystemLog> it=list.iterator();
			while(it.hasNext())
				log.add( it.next().getLogEvent());
		}
		se.close();
		return log;
	}
}
